package com.thread;

import java.util.concurrent.ConcurrentHashMap;

public class WaitLock {

	static ConcurrentHashMap<String, Object> map = new ConcurrentHashMap<String, Object>();
	static Object lock = new Object();
	
	public static void main(String[] args) {
		
		for(int i=1;i<10;i++){
			new TestThread("1").start();
		}
	}

	public static void test(String key) throws InterruptedException{
		
		synchronized (lock) {
			if(!map.containsKey(key)){
				map.put(key,new Object());
			}
		}
		
		synchronized (map.get(key)) {
			if(!map.containsKey(key)){
				System.out.println("out of key");
				return;
			}
			System.out.println("key:"+key);
			Thread.sleep(3000);
			map.remove(key);
		}
	}
	
}


class TestThread extends Thread{
	private String key;
	
	public TestThread(String key) {
		this.key = key;
	}
	@Override
	public void run() {
		try {
			WaitLock.test(key);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
